package com.xmindguoguo.boot.modular.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xmindguoguo.boot.common.annotation.OperationLog;
import com.xmindguoguo.boot.common.constant.GlobalConstant;
import com.xmindguoguo.boot.common.enumtype.OperationLogType;
import com.xmindguoguo.boot.core.BaseController;
import com.xmindguoguo.boot.core.shiro.ShiroKit;
import com.xmindguoguo.boot.modular.system.model.TSystemDeptModel;
import com.xmindguoguo.boot.modular.system.service.ITSystemDeptService;
import com.xmindguoguo.boot.util.CmUtil;
import com.xmindguoguo.boot.util.ConvertUtil;
import com.xmindguoguo.boot.util.ToolUtil;
import com.xmindguoguo.boot.util.json.JsonResult;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author xmind-guoguo
 * @since 2018-11-04
 */
@Controller
@RequestMapping("/systemDept")
public class SystemDeptController extends BaseController {
    @Autowired
    public ITSystemDeptService systemDeptService;

    /**
     * 跳转到部门树列表
     * 
     * @Title toSystemUserList
     * @author 于国帅
     * @date 2018年11月28日 上午10:33:53
     * @return String
     */
    @RequestMapping("toSystemDeptList")
    public String toSystemUserList() {
        return "system/systemDept/systemDeptList"; // 跳转到列表页
    }

    // 查询tree 列表数据 加上分页
    @RequestMapping("systemDeptList")
    @ResponseBody
    public Object systemDeptList(TSystemDeptModel model) {
        Wrapper<TSystemDeptModel> wrapper = new EntityWrapper<>();
        if (ToolUtil.isNotEmpty(model.getSimpleName())) {
            wrapper.eq("simpleName", model.getSimpleName());
            wrapper.or();
            wrapper.eq("fullName", model.getSimpleName());
        }
        wrapper.ne("status", GlobalConstant.MODEL_DEL);
        wrapper.orderBy("createTime,orderNum ", false);
        return JsonResult.sendOk(systemDeptService.selectList(wrapper));
    }

    @RequestMapping("toSystemDeptAdd")
    public String toSystemUserAdd() {
        return "system/systemDept/systemDeptAdd"; // 跳转到列表页
    }

    @OperationLog(logName = "添加部门", operationLogType = OperationLogType.INSERT)
    @RequestMapping("systemDeptAdd")
    @ResponseBody
    public Object systemDeptAdd(@Validated TSystemDeptModel model, BindingResult result) {
        if (result.hasErrors()) {
            return sendValidatedMsg(result);
        }
        model.setId(CmUtil.getPkId());
        model.setStatus(GlobalConstant.MODEL_ADD);
        model.setCreateTime(new Date());
        model.setCreateUserId(ShiroKit.getUserId());
        setPids(model);
        boolean flag = systemDeptService.insert(model);
        return flag ? JsonResult.sendOk("添加成功", null) : JsonResult.sendError("添加失败", null);
    }

    private void setPids(TSystemDeptModel model) {
        if (model.getPId() == null) {
            model.setPId(0L);
            model.setPIds(model.getId() + "");
        }
        if (model.getPId() != 0L) {
            TSystemDeptModel oldModel = systemDeptService.selectById(model.getPId());
            model.setPIds(oldModel.getPIds() + "_" + model.getId());
        }
    }

    @RequestMapping("toSystemDeptUpdate")
    public String toSystemDeptUpdate(Long id) {
        TSystemDeptModel model = systemDeptService.selectById(id);
        request.setAttribute("model", model);
        // 查询出来对应的上级部门名称
        if (model != null && model.getPId() != 0) {
            TSystemDeptModel pModel = systemDeptService.selectById(model.getPId());
            request.setAttribute("showNameDom", pModel.getFullName());
        } else {
            request.setAttribute("showNameDom", "顶级");
        }
        return "system/systemDept/systemDeptUpdate"; // 跳转到列表页
    }

    @OperationLog(logName = "更新部门", operationLogType = OperationLogType.UPDATE)
    @RequestMapping("systemDeptUpdate")
    @ResponseBody
    public Object systemUserUpdate(@Validated TSystemDeptModel model, BindingResult result) {
        if (result.hasErrors()) {
            return sendValidatedMsg(result);
        }
        model.setUpdateTime(new Date());
        model.setUpdateUserId(ShiroKit.getUserId());

        setPids(model);

        boolean flag = systemDeptService.updateById(model);
        return flag ? JsonResult.sendOk("更新成功", null) : JsonResult.sendError("更新失败", null);
    }

    @OperationLog(logName = "删除部门", operationLogType = OperationLogType.DELETE)
    @RequestMapping("systemDeptDelete")
    @ResponseBody
    public Object systemDeptDelete(Long id) {
        TSystemDeptModel model = new TSystemDeptModel();
        model.setStatus(GlobalConstant.MODEL_DEL);
        model.setId(id);
        model.setUpdateTime(new Date());
        model.setUpdateUserId(ShiroKit.getUserId());
        boolean flag = systemDeptService.updateById(model);
        return flag ? JsonResult.sendOk("删除成功", null) : JsonResult.sendError("删除失败", null);
    }

    @OperationLog(logName = "批量删除部门", operationLogType = OperationLogType.DELETE)
    @RequestMapping("systemDeptBatchDel")
    @ResponseBody
    public Object systemDeptBatchDel(String ids) {
        Long[] idsArray = ConvertUtil.StringToLongArray(ids);
        List<TSystemDeptModel> list = new ArrayList<>();
        TSystemDeptModel model = null;
        for (Long id : idsArray) {
            model = new TSystemDeptModel();
            model.setStatus(GlobalConstant.MODEL_DEL);
            model.setUpdateTime(new Date());
            model.setUpdateUserId(ShiroKit.getUserId());
            model.setId(id);
            list.add(model);
        }
        boolean flag = systemDeptService.updateBatchById(list);
        return flag ? JsonResult.sendOk("删除成功", null) : JsonResult.sendError("删除失败", null);
    }

}
